package cn.jimmie.learn.viewArt.refreshview.core.scrollview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

/**
 * function :
 *
 * <p> Create Time:  2019年04月14日 17:10 </p>
 * <p> @author Jimmie.Qian  </p>
 */
public class RefreshScrollView extends ScrollView {

    private OnOverScrollListener mListener;
    private View mFootView;

    public RefreshScrollView(Context context) {
        super(context);
    }

    public RefreshScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RefreshScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void addView(View child) {
        this.addView(child, getLastChildIndex());
    }

    @Override
    public void addView(View child, int index) {
        if (getChildCount() == 0) {
            super.addView(child, index);
            return;
        }

        ViewGroup layout = getChildLayout();
        if (layout == null) return;

        int realIndex = index;
        // 在最后插入 // footView 不为空
        if (index == layout.getChildCount() - 1 && mFootView != null) {
            realIndex--;
        }
        layout.addView(child, realIndex);
        setFootView(mFootView);
    }

    @Override
    public void addView(View child, ViewGroup.LayoutParams params) {
        this.addView(child, getLastChildIndex(), params);
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        if (getChildCount() == 0) {
            super.addView(child, index, params);
            return;
        }

        ViewGroup layout = getChildLayout();
        if (layout == null) return;

        int realIndex = index;
        // 在最后插入 // footView 不为空
        if (index == layout.getChildCount() - 1 && mFootView != null) {
            realIndex--;
        }
        layout.addView(child, realIndex, params);
        setFootView(mFootView);
    }

    @Override
    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
        if (mListener != null) mListener.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
    }

    private int getLastChildIndex() {
        ViewGroup layout = getChildLayout();
        if (layout == null) return 0;

        return layout.getChildCount();
    }

    private ViewGroup getChildLayout() {
        int index = getChildCount();
        if (index != 1) return null;

        View view = getChildAt(0);
        if (!(view instanceof ViewGroup)) return null;

        return (ViewGroup) getChildAt(0);
    }

    void setFootView(View footView) {
        if (footView == null) return;

        ViewGroup layout = getChildLayout();
        if (layout == null) return;

        if (mFootView != null) {
            layout.removeView(footView);
        }
        mFootView = footView;
        layout.addView(footView);
    }

    void removeFootView() {
        if (mFootView == null) return;

        ViewGroup layout = getChildLayout();
        if (layout == null) return;

        layout.removeView(mFootView);
        mFootView = null;
    }

    void setOverScrollListener(OnOverScrollListener listener) {
        this.mListener = listener;
    }

    /**
     * 是否占满一屏
     */
//    boolean overFullLayout() {
//        ViewGroup layout = (ViewGroup) getChildAt(0);
//        int childHeight = layout.getPaddingBottom() + layout.getPaddingTop();
//        int count = layout.getChildCount();
//        for (int i = 0; i < count; i++) {
//            int margin = 0;
//            View child = layout.getChildAt(i);
//            ViewGroup.LayoutParams params = child.getLayoutParams();
//            if (params instanceof MarginLayoutParams) {
//                MarginLayoutParams tmp = (MarginLayoutParams) params;
//                margin = tmp.bottomMargin + tmp.topMargin;
//            }
//            childHeight += child.getHeight() + margin;
//        }
//        return childHeight > getHeight();
//    }

    interface OnOverScrollListener {
        void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY);
    }
}
